import db from './db'
import { mock } from 'mockjs'
import { orderBy } from '@/utils'

// POST 产品分类列表
mock('productcate/list', 'post', ( payload ) => {

  const { page, pageSize, state, keyword } = JSON.parse(payload.body)

  const data = orderBy( db.select('gx_productcate') );

  const searchData = data.filter( item => {
      return (state ? item.state === state : true) && 
               (keyword ? item.title.includes(keyword) : true);
  })

  let totalpage = Math.ceil(searchData.length / pageSize);

  let start = (page - 1) * pageSize;
  let end = start + pageSize;

  const pageData = searchData.slice(start, end);

  return {
    err: 0,
    desc: '执行成功！',
    info: {
      result: pageData,
      totalpage
    } 
  }
  
})

// POST 产品分类状态
mock('productcate/state', 'post', ( payload ) => {
  const { id, state } = JSON.parse(payload.body)
  return {
    err: 0,
    desc: '执行成功！',
    result: db.put ('gx_productcate', id, { state })
  }
})

// POST 产品分类删除
mock('productcate/remove', 'post', ( payload ) => {
  const { ids } = JSON.parse(payload.body);
  return {
    err: 0,
    desc: '执行成功！',
    result: db.delete ('gx_productcate', ids)
  }
})

// POST 产品分类新增
mock('productcate/add', 'post', ( payload ) => {
  const form = JSON.parse(payload.body)
  return {
    err: 0,
    desc: '执行成功！',
    result: db.add ('gx_productcate', form)
  }
})

// POST 产品分类修改
mock('productcate/edit', 'post', ( payload ) => {
  const {id, ...others} = JSON.parse(payload.body)
  return {
    err: 0,
    desc: '执行成功！',
    result: db.put('gx_productcate', id, others)
  }
})